home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zstring.c < prev    next >
C/C++ Source or Header  |  1993-05-28  |  5KB  |  186 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zstring.c */
  20. /* String operators for Ghostscript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "stream.h"
  24. #include "alloc.h"
  25. #include "errors.h"
  26. #include "iname.h"
  27. #include "iscan.h"
  28. #include "iutil.h"
  29. #include "oper.h"
  30. #include "store.h"
  31.  
  32. /* The generic operators (copy, get, put, getinterval, putinterval, */
  33. /* length, and forall) are implemented in zgeneric.c. */
  34.  
  35. /* Imported operators */
  36. extern int ztoken_file(P1(os_ptr));
  37.  
  38. /* <int> string <string> */
  39. int
  40. zstring(register os_ptr op)
  41. {    byte *sbody;
  42.     uint size;
  43.     check_type(*op, t_integer);
  44.     if ( op->value.intval < 0 || (ulong)(op->value.intval) > max_ushort )
  45.         return_error(e_rangecheck);
  46.     size = op->value.intval;
  47.     sbody = (byte *)alloc(size, 1, "string");
  48.     if ( sbody == 0 )
  49.         return_error(e_VMerror);
  50.     make_string(op, a_all, size, sbody);
  51.     memset(sbody, 0, size);
  52.     return 0;
  53. }
  54.  
  55. /* <string> <pattern> anchorsearch <post> <match> true */
  56. /* <string> <pattern> anchorsearch <string> false */
  57. int
  58. zanchorsearch(register os_ptr op)
  59. {    os_ptr op1 = op - 1;
  60.     uint size = r_size(op);
  61.     check_read_type(*op1, t_string);
  62.     check_read_type(*op, t_string);
  63.     if ( size <= r_size(op1) && !memcmp(op1->value.bytes, op->value.bytes, size) )
  64.     {    os_ptr op0 = op;
  65.         push(1);
  66.         *op0 = *op1;
  67.         r_set_size(op0, size);
  68.         op1->value.bytes += size;
  69.         r_inc_size(op1, -size);
  70.         make_bool(op, 1);
  71.     }
  72.     else
  73.         make_bool(op, 0);
  74.     return 0;
  75. }
  76.  
  77. /* <string> <pattern> search <post> <match> <pre> true */
  78. /* <string> <pattern> search <string> false */
  79. int
  80. zsearch(register os_ptr op)
  81. {    os_ptr op1 = op - 1;
  82.     uint size = r_size(op);
  83.     uint count;
  84.     byte *ptr;
  85.     check_read_type(*op1, t_string);
  86.     check_read_type(*op, t_string);
  87.     if ( size > r_size(op1) )        /* can't match */
  88.        {    make_bool(op, 0);
  89.         return 0;
  90.        }
  91.     count = r_size(op1) - size;
  92.     ptr = op1->value.bytes;
  93.     do
  94.        {    if ( !memcmp(ptr, op->value.bytes, size) )
  95.            {    op->tas.type_attrs = op1->tas.type_attrs;
  96.             op->value.bytes = ptr;
  97.             r_set_size(op, size);
  98.             push(2);
  99.             op[-1] = *op1;
  100.             r_set_size(op - 1, ptr - op[-1].value.bytes);
  101.             op1->value.bytes = ptr + size;
  102.             r_set_size(op1, count);
  103.             make_bool(op, 1);
  104.             return 0;
  105.            }
  106.         ptr++;
  107.        }
  108.     while ( count-- );
  109.     /* No match */
  110.     make_bool(op, 0);
  111.     return 0;
  112. }
  113.  
  114. /* <obj> <pattern> .stringmatch <bool> */
  115. int
  116. zstringmatch(register os_ptr op)
  117. {    os_ptr op1 = op - 1;
  118.     int result;
  119.     check_read_type(*op, t_string);
  120.     switch ( r_type(op1) )
  121.        {
  122.     case t_string:
  123.         check_read(*op1);
  124.         goto cmp;
  125.     case t_name:
  126.         name_string_ref(op1, op1);        /* can't fail */
  127. cmp:        result = string_match(op1->value.const_bytes, r_size(op1),
  128.                       op->value.const_bytes, r_size(op),
  129.                       NULL);
  130.         break;
  131.     default:
  132.         result = (r_size(op) == 1 && *op->value.bytes == '*');
  133.        }
  134.     make_bool(op1, result);
  135.     pop(1);
  136.     return 0;
  137. }
  138.  
  139. /* <string|file> token <post> <obj> true */
  140. /* <string|file> token false */
  141. int
  142. ztoken(register os_ptr op)
  143. {    stream st;
  144.     stream *s = &st;
  145.     int code;
  146.     ref token;
  147.     switch ( r_type(op) )
  148.        {
  149.     default:
  150.         return_error(e_typecheck);
  151.     case t_file:
  152.         return ztoken_file(op);
  153.     case t_string: ;
  154.        }
  155.     check_read(*op);
  156.     sread_string(s, op->value.bytes, r_size(op));
  157.     switch ( code = scan_token(s, 1, &token) )
  158.        {
  159.     default:            /* possible error */
  160.         if ( code < 0 ) return code;
  161.                     /* read a token */
  162.        {    uint pos = stell(s);
  163.         op->value.bytes += pos;
  164.         r_inc_size(op, -pos);
  165.        }
  166.         push(2);
  167.         op[-1] = token;
  168.         make_bool(op, 1);
  169.         return 0;
  170.     case scan_EOF:            /* no tokens */
  171.         make_bool(op, 0);
  172.         return 0;
  173.        }
  174. }
  175.  
  176. /* ------ Initialization procedure ------ */
  177.  
  178. op_def zstring_op_defs[] = {
  179.     {"2anchorsearch", zanchorsearch},
  180.     {"2search", zsearch},
  181.     {"1string", zstring},
  182.     {"2.stringmatch", zstringmatch},
  183.     {"1token", ztoken},
  184.     op_def_end(0)
  185. };
  186.